package com.piece.blog.service;

import com.piece.blog.model.TrainingQuestionItem;
import com.piece.blog.repository.TrainingQuestionItemRepository;
import com.piece.core.framework.util.collection.MapUtil;
import com.piece.core.jpa.repository.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.HashMap;

@Service
@Transactional
public class TrainingQuestionItemService extends BaseService<TrainingQuestionItem, String> {

    @Autowired
    private TrainingQuestionItemRepository trainingQuestionItemRepository;

    @PostConstruct
    public void initRepository() {
        setRepository(trainingQuestionItemRepository);
    }

    public boolean deleteByQuestion(Long questionId) {
        trainingQuestionItemRepository.deleteByProperties("questionId", questionId);
        return true;
    }

    public boolean deleteByQuestion(Long[] questionId) {
        trainingQuestionItemRepository.deleteByProperties(MapUtil.builder(new HashMap<String, Object>())
                .put("Q_questionId_L_IN", questionId)
                .build());
        return true;
    }
}
